package Controller;

import Entity.Experiment;
import Entity.Room;
import Service.ExperimentService;
import Service.RoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AllExpServlet extends HttpServlet {
    public AllExpServlet(){super();}

    public void destroy(){super.destroy();}

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);
        String uname = (String)session.getAttribute("uname");
        String uid = (String)session.getAttribute("uid");
        if(session == null){
            request.getRequestDispatcher("/WEB-INF/page/Login.jsp").forward(request,response);
        }
        
        String op=(String)request.getAttribute("op");
        if("mainpage".equals(op)){
        	request.setAttribute("uname",uname);
        	request.setAttribute("uid",uid);
        	request.getRequestDispatcher("/WEB-INF/page/StudentPage.jsp").forward(request,response);
        	return;
        }
        String operation=request.getParameter("operation");
        if("mainpage".equals(operation)){
        	request.getRequestDispatcher("/WEB-INF/page/StudentPage.jsp").forward(request,response);
        	return;
        }
        
        ExperimentService es = new ExperimentService();
        RoomService rs = new RoomService();
        List<Room> rlist = rs.findAllRoom();
        List<Room> r1=new ArrayList<Room>(),r2=new ArrayList<Room>();
        List<Experiment> elist = es.findAllExperiment();
        for(Room r:rlist){
            if(r.getRoomtype() == 1) r1.add(r);
            else r2.add(r);
        }
        System.out.println( elist.get(0).getTeURL() );
        request.setAttribute("elist",elist);
        request.setAttribute("rlist1",r1);
        request.setAttribute("rlist2",r2);
        request.setAttribute("uname",uname);
        request.setAttribute("uid",uid);
        request.getRequestDispatcher("/WEB-INF/page/AllExp.jsp").forward(request,response);
    }
}
